<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/


/** Implements some functionality for tag module
 * @package module.tag
 * @version $Id: PhpFox_ServiceTag.class.php 309 2008-02-12 05:36:32Z phpfoxsv $
 *
 */
class PhpFox_Mod_Tag_ServiceTag extends BaseService
{
 	function displayTags($sType = "")
 	{
   	 	$oCache = &App::newObject("Cache");
   	 	$oCache->setFile("tags_". $sType.".php");
   	 	if ( $oCache->isCached(App::getParam('vid_doCache')) )
   	 	{
   	 	 	$tags = $oCache->doRequire();
   	 	}
		else
		{
			$tags = $this->createTags($sType, $oCache);
		}

		if ( empty($tags) )
		{
			return '';
		}

		return $this->prepareTags($tags, $sType);
 	}

 	function createTags($sType = "", &$oCache)
 	{
 		static $aCachedTags;

 		if ( isset($aCachedTags[$sType]) )
 		{
 			return $aCachedTags[$sType];
 		}

		switch($sType)
		{
			case 'blog':
			$sTagTable = "journal";
			$sOrderBy = "time";
			$sTagName = "tags";
			break;
			case 'videos':
			$sTagTable = "videos";
			$sOrderBy = "vid_time";
			$sTagName = "vid_tags";
			break;
			case 'gallery':
			$sTagTable = "main";
			$sOrderBy = "time";
			$sTagName = "tags";
			break;
			default:
			$sTagTable = "";
			$sOrderBy = "";
			$sTagName = "";
			break;
		}

		$aTags = $this->oDatabase->getRows("SELECT ". $sTagName ." as tag FROM ". App::getT($sTagTable) ."
			WHERE ". $sTagName ." != ''
			ORDER BY ". $sOrderBy ." DESC, ". $sTagName ." ASC LIMIT ". App::getParam('vid_total_check', '', '50') ."");

		$aCachedTags[$sType] = $aTags;

		if ( empty($aTags) )
		{
			return '';
		}

		$sBanedTags = $this->parseTags();

		$tags = array();
		foreach($aTags as $row)
		{
			$row['tag'] = strip_tags($row['tag']);
    		$temp_tags = explode(' ', $row['tag']);
		    foreach ($temp_tags as $tag)
		    {
		    	if ( !in_array($tag, $sBanedTags) && (strlen($tag) >= App::getParam('vid_min_string', '', '3')) )
		    	{
	        		$tag = strtolower($tag);
	        		if (isset($tags[$tag]))
	        		{
	            		$tags[$tag]++;
	        		}
	        		else
	        		{
	            		$tags[$tag] = 1;
	        		}
        		}
    		}
		}

		if ( count($tags) == 0 )
		{
			return '';
		}

		arsort($tags);

		$aReplace = array
		(
			'(',
			')',
			',',
			'!',
			'?',
			'/',
			'>',
			'<',
			':'
		);

		$nCount = 0;
		$aFinalTags = array();
		foreach($tags as $key => $value)
		{
			$nCount++;
			if ( $nCount > App::getParam('tag_total_display') )
			{
				break;
			}

			$key = ucwords(str_replace($aReplace, '', $key));
			if (substr($key, -1) == '.')
			{
				$key = rtrim($key, '.');
			}

			if (is_numeric($key))
			{
				continue;
			}

			$aFinalTags[$key] = $value;
		}
		
		ksort($aFinalTags);
		$content = "";
		foreach ($aFinalTags as $aFinalKey => $aFinalValue) 
		{
			if (empty($aFinalKey))
			{
				continue;
			}			
			$content .= '\''. trim(str_replace("'", "\'", $aFinalKey)) .'\' => '. str_replace("'", "\'", $aFinalValue) .',';
		}

		$oCache->save($content);
   	 	return $oCache->doRequire();
 	}

 	function parseTags()
 	{
   	 	$oCache = &App::newObject("Cache");
   	 	$oCache->setFile("tags_ban.php");
   	 	if ( $oCache->isCached(App::getParam('vid_doCache')) )
   	 	{
   	 	 	$tags_ban = $oCache->doRequire();
   	 	}
		else
		{
			$tag_bans = $this->oDatabase->getRows("SELECT phrase FROM ". App::getT('ban_tag') ."");
			$content = "";
			foreach($tag_bans as $ban)
			{
				$content .= '\''. str_replace("'", "\'", $ban['phrase']) .'\',';
			}
			$oCache->save($content);
   	 		$tags_ban = $oCache->doRequire();
		}
		return $tags_ban;
 	}

 	function prepareTags($tags, $sType)
 	{
		if ( count($tags) <= App::getParam('tag_min_display') )
 		{
 			return false;
 		}

 		$sLlink = "";

 		switch($sType)
 		{
 			case 'blog':
 			$sLink = "public.blog.main";
 			break;
 			case 'videos':
 			$sLink = "public.videos";
 			break;
 			case 'gallery':
 			$sLink = "public.gallery.main";
 			break;
 		}

 		$max_size = App::getParam('vid_px_max_size');
        $min_size = App::getParam('vid_px_min_size');

        $max_qty = max(array_values($tags));
        $min_qty = min(array_values($tags));

        $spread = $max_qty - $min_qty;
        if ($spread == 0)
        {
			$spread = 1;
        }
        $step = ($max_size - $min_size) / ($spread);

        $aTempTags = array();
        foreach ($tags as $key => $value)
        {
			$size = round($min_size + (($value - $min_qty) * $step));
			$key = str_replace(',', '', $key);
            $aTempTags[] = array
			(
				'size' => $size,
                'value' => $value,
                'key' => $key,
            	'link' => App::makeUrl($sLink, array('tag' => App::cleanUrl(str_replace('.', '-point-', $key), false)))
			);
        }
		return $aTempTags;
	}
}

?>